Pythonでミニゲーム作成



作ろうとした物



Pythonが元々人工知能などを作れることを知っていた自分は某アメコミの鉄男さんの作中に出てくるAIもどきを作ってみようと思った。

まず制作にあたり、必要なものをリストアップした。
  • 音声認識ツール
  • 音声変換ツール
  • 音声発声ツール
  • Xcode,etc.....



  • などなどである。Xcodeがリストに入ってるのはその作ったものをXcodeを通してiPhoneに入れられないかと考えたためである。
    調べてみたところ、Pythonを使ってもXcodeを通してiPhone自体に入れることは可能とのことだったため採用した。
    そして、制作開始をした。


    とりあえずどの場面で役立つものにするかを考えて、コードを書いて。
    残りは音声認識〜音声発声ツールだった。
    まず、「Speech Recognition」と言うツールをダウンロードし、その上で音声認識エンジン/APIをダウンロードまたは利用しようと試みた。
    自分は「Google Speech Recognition」を採用したのだが、ここで問題が発生した。
    「 Speech Recognition」だけでは動かず、「Google Speech Recognition」だけでも動かず、その二つを繋ぐ「pyaudio」と言うソフトがあり、ダウンロードを試みたものの、まさかのサービスが終了していた。
    その後他の「pyaudio」に代わる物を探したが発見できず、今回制作しようとしたものは没になってしまった。

    ※pyaudioがあった場合ダウンロードした後最初に実行しようとしてたコードを貼っておきます。

    1. from __future__ import division
    2. import re
    3. import sys
    4. from google.cloud import speech
    5. import pyaudio
    6. from six.moves import queue
    7. # Audio recording parameters
    8. RATE = 16000
    9. CHUNK = int(RATE / 10) # 100ms
    10. class MicrophoneStream(object):
    11.     """Opens a recording stream as a generator yielding the audio chunks."""
    12.     def __init__(self, rate, chunk):
    13.         self._rate = rate
    14.         self._chunk = chunk
    15.         # Create a thread-safe buffer of audio data
    16.         self._buff = queue.Queue()
    17.         self.closed = True
    18.     def __enter__(self):
    19.         self._audio_interface = pyaudio.PyAudio()
    20.         self._audio_stream = self._audio_interface.open(
    21.             format=pyaudio.paInt16,
    22.             # The API currently only supports 1-channel (mono) audio
    23.             # https://goo.gl/z757pE
    24.             channels=1,
    25.             rate=self._rate,
    26.             input=True,
    27.             frames_per_buffer=self._chunk,
    28.             # Run the audio stream asynchronously to fill the buffer object.
    29.             # This is necessary so that the input device's buffer doesn't
    30.             # overflow while the calling thread makes network requests, etc.
    31.             stream_callback=self._fill_buffer,
    32.         )
    33.         self.closed = False
    34.         return self
    35.     def __exit__(self, type, value, traceback):
    36.         self._audio_stream.stop_stream()
    37.         self._audio_stream.close()
    38.         self.closed = True
    39.         # Signal the generator to terminate so that the client's
    40.         # streaming_recognize method will not block the process termination.
    41.         self._buff.put(None)
    42.         self._audio_interface.terminate()
    43.     def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
    44.         """Continuously collect data from the audio stream, into the buffer."""
    45.         self._buff.put(in_data)
    46.         return None, pyaudio.paContinue
    47.     def generator(self):
    48.         while not self.closed:
    49.             # Use a blocking get() to ensure there's at least one chunk of
    50.             # data, and stop iteration if the chunk is None, indicating the
    51.             # end of the audio stream.
    52.             chunk = self._buff.get()
    53.             if chunk is None:
    54.                 return
    55.             data = [chunk]
    56.             # Now consume whatever other data's still buffered.
    57.             while True:
    58.                 try:
    59.                     chunk = self._buff.get(block=False)
    60.                     if chunk is None:
    61.                         return
    62.                     data.append(chunk)
    63.                 except queue.Empty:
    64.                     break
    65.             yield b"".join(data)
    66. def listen_print_loop(responses):
    67.     """Iterates through server responses and prints them.
    68.     The responses passed is a generator that will block until a response
    69.     is provided by the server.
    70.     Each response may contain multiple results, and each result may contain
    71.     multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we
    72.     print only the transcription for the top alternative of the top result.
    73.     In this case, responses are provided for interim results as well. If the
    74.     response is an interim one, print a line feed at the end of it, to allow
    75.     the next result to overwrite it, until the response is a final one. For the
    76.     final one, print a newline to preserve the finalized transcription.
    77.     """
    78.     num_chars_printed = 0
    79.     for response in responses:
    80.         if not response.results:
    81.             continue
    82.         # The `results` list is consecutive. For streaming, we only care about
    83.         # the first result being considered, since once it's `is_final`, it
    84.         # moves on to considering the next utterance.
    85.         result = response.results[0]
    86.         if not result.alternatives:
    87.             continue
    88.         # Display the transcription of the top alternative.
    89.         transcript = result.alternatives[0].transcript
    90.         # Display interim results, but with a carriage return at the end of the
    91.         # line, so subsequent lines will overwrite them.
    92.         #
    93.         # If the previous result was longer than this one, we need to print
    94.         # some extra spaces to overwrite the previous result
    95.         overwrite_chars = " " * (num_chars_printed - len(transcript))
    96.         if not result.is_final:
    97.             sys.stdout.write(transcript + overwrite_chars + "\r")
    98.             sys.stdout.flush()
    99.             num_chars_printed = len(transcript)
    100.         else:
    101.             print(transcript + overwrite_chars)
    102.             # Exit recognition if any of the transcribed phrases could be
    103.             # one of our keywords.
    104.             if re.search(r"\b(exit|quit)\b", transcript, re.I):
    105.                 print("Exiting..")
    106.                 break
    107.             num_chars_printed = 0
    108. def main():
    109.     # See http://g.co/cloud/speech/docs/languages
    110.     # for a list of supported languages.
    111.     language_code = "en-US" # a BCP-47 language tag
    112.     client = speech.SpeechClient()
    113.     config = speech.RecognitionConfig(
    114.         encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
    115.         sample_rate_hertz=RATE,
    116.         language_code=language_code,
    117.     )
    118.     streaming_config = speech.StreamingRecognitionConfig(
    119.         config=config, interim_results=True
    120.     )
    121.     with MicrophoneStream(RATE, CHUNK) as stream:
    122.         audio_generator = stream.generator()
    123.         requests = (
    124.             speech.StreamingRecognizeRequest(audio_content=content)
    125.             for content in audio_generator
    126.         )
    127.         responses = client.streaming_recognize(streaming_config, requests)
    128.         # Now, put the transcription responses to use.
    129.         listen_print_loop(responses)
    130. if __name__ == "__main__":
    131.     main()